﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NS_QuangCaoTiepThiBUS
{
    public class NguoiDungBUS
    {
        public void Them(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            storeDB.NguoiDungs.InsertOnSubmit(ndDTO);

            storeDB.SubmitChanges();
        }

        public void Sua(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung).First();

            query.TenNguoiDung = ndDTO.TenNguoiDung;
            query.MaPhong = ndDTO.MaPhong;
            query.MaLoaiNguoiDung = ndDTO.MaLoaiNguoiDung;
            query.MaChucVu = ndDTO.MaChucVu;

            storeDB.SubmitChanges();
        }

        public void Xoa(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung).First();

            storeDB.NguoiDungs.DeleteOnSubmit(query);

            storeDB.SubmitChanges();
        }

        public IList<NguoiDung> LayDanhSachNguoiDung()
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.ToList();

            return query;
        }

        public bool KiemTraTonTaiNguoiDung(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung).ToList();

            if (query.Count > 0)
            {
                return true;
            }

            return false;
        }

        public IList<NguoiDung> LayThongTinNguoiDungTheoMaNguoiDung(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung).ToList();

            return query;
        }

        public void DoiMatKhau(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung).First();

            query.MatKhau = ndDTO.MatKhau;

            storeDB.SubmitChanges();
        }

        public string LayMaLoaiNguoiDung(NguoiDung ndDTO)
        {
            QCTTDataContext storeDB = new QCTTDataContext();

            var query = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung && q.MatKhau == ndDTO.MatKhau).ToList();

            if (query.Count > 0)
            {
                var query2 = storeDB.NguoiDungs.Where(q => q.MaNguoiDung == ndDTO.MaNguoiDung && q.MatKhau == ndDTO.MatKhau).Select(q => q.MaLoaiNguoiDung).First();

                return query2;
            }

            return null;
        }
    }
}
